package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.company.Company;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.company.ICompanyService;
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公司信息Controller
 * Created by weih on 2016-10-19.
 */
@Controller
@RequestMapping("/mdm/company")
public class CompanyController extends BaseController {

    @Autowired
    ICompanyService companyService;
    /**
     * 跳转新增公司页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/company/add";
    }
    /**
     * 新增公司信息
     * @param company 公司对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addCompany(@RequestBody Company company,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        company.setCreateUser(getSessionUser().getUserId());
        company.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            companyService.addCompany(company);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑公司页面
     * @param companyCode  公司代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String companyCode,Model model){
        model.addAttribute("company", companyService.getCompanyByCode(companyCode));
        return "mdm/company/edit";
    }

    /**
     * 编辑公司信息
     * @param company
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editCompany(@RequestBody Company company,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Company oldCompany=companyService.getCompanyByCode(company.getCompanyCode());
        oldCompany.setId(company.getId());
        oldCompany.setCompanyCode(company.getCompanyCode());
        oldCompany.setCompanyName(company.getCompanyName());
        oldCompany.setCompanyType(company.getCompanyType());
        oldCompany.setCompanyShortName(company.getCompanyShortName());
        oldCompany.setCompanyProp(company.getCompanyProp());
        oldCompany.setParentId(company.getParentId());
        oldCompany.setCompanyIndustry(company.getCompanyIndustry());
        oldCompany.setLicenseNo(company.getLicenseNo());
        oldCompany.setLicenseNoImg(company.getLicenseNoImg());
        oldCompany.setTaxNo(company.getTaxNo());
        oldCompany.setAjCompanyClass(company.getAjCompanyClass());
        oldCompany.setCorporation(company.getCorporation());
        oldCompany.setCeo(company.getCeo());
        oldCompany.setCeoIdcard(company.getCeoIdcard());
        oldCompany.setCeoMobile(company.getCeoMobile());
        oldCompany.setCeoIdcardAttachA(company.getCeoIdcardAttachA());
        oldCompany.setCeoIdcardAttachB(company.getCeoIdcardAttachB());
        oldCompany.setCertificateStatus(company.getCertificateStatus());
        oldCompany.setCertificateTime(company.getCertificateTime());
        oldCompany.setCertificatedTime(company.getCertificatedTime());
        oldCompany.setStatus(company.getStatus());
        oldCompany.setCountryCode(company.getCountryCode());
        oldCompany.setProvinceCode(company.getProvinceCode());
        oldCompany.setCityCode(company.getCityCode());
        oldCompany.setCountyCode(company.getCountyCode());
        oldCompany.setPostcode(company.getPostcode());
        oldCompany.setStreet(company.getStreet());
        oldCompany.setAddress(company.getAddress());
        oldCompany.setBankName(company.getBankName());
        oldCompany.setBankAccount(company.getBankAccount());
        oldCompany.setRemark(company.getRemark());
        oldCompany.setUpdateUser(getSessionUser().getUserId());
        oldCompany.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            companyService.updateCompany(oldCompany);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除公司信息
     * @param company 公司对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteCompany(@RequestBody Company company,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        company.setUpdateUser(getSessionUser().getUserId());
        company.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            companyService.deleteCompany(company);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

	/**
     * 跳转公司明细页面
     * @param companyCode  大区代码
     */
    @RequestMapping("/detail")
    public String toDetailPage(String companyCode,Model model){
        Company company = companyService.getCompanyByCode(companyCode);
        model.addAttribute("company", company);

        model.addAttribute("nation", regionService.getRegionByCode(company.getNationCode()));
        return "mdm/company/detail";
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/company/list";
    }

    /**
     * 分页查询数据
     * @param company 公司对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Company> getCompanyData(Company company ,DataTableRequest dtrequest){
        int cnt = (int)companyService.getCompanyCount(company);
        List<Company> list = companyService.queryCompanys(company, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Company> result = new PaginationResult<Company>(list, pi);
        return new DataTableResponse<Company>(dtrequest.getsEcho(),result);
    }

}
